package com.leo.lime.music.content.me.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.leo.lime.music.R;
import com.leo.lime.music.content.me.domain.Promotion;
import com.leo.lime.music.content.me.utils.MeUtils;

import java.util.ArrayList;

public class PromotionAdapter implements ExpandableListAdapter {
    private static final String TAG = "ContentMePromotionAdapter";

    private Context mContext;
    ArrayList<Promotion> mPromotions;
    private State mState = State.UNREGISTER;

    private LayoutInflater mLInflater;

    private enum State {
        REGISTER, UNREGISTER
    }

    public PromotionAdapter(Context ctx, ArrayList<Promotion> promotions) {
        this(ctx, promotions, State.UNREGISTER);
    }

    public PromotionAdapter(Context ctx, State state) {
        this(ctx, null, state);
    }

    public PromotionAdapter(Context ctx, ArrayList<Promotion> promotions, State state) {
        mContext = ctx;
        mPromotions = promotions;
        mState = state;
        mLInflater = LayoutInflater.from(mContext);
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return mPromotions.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mPromotions != null && mPromotions.size() > 0 && mPromotions.get(groupPosition) != null && mPromotions.get(groupPosition).getChilds() != null) {
            return mPromotions.get(groupPosition).getChilds().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mPromotions == null || mPromotions.size() <= 0) {
            return null;
        }
        return mPromotions.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mPromotions == null || mPromotions.size() <= 0 || mPromotions.get(groupPosition) == null || mPromotions.get(groupPosition).getChilds() == null) {
            return null;
        }
        return mPromotions.get(groupPosition).getChilds().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (mState == State.REGISTER) {
            return setupRegisterView(groupPosition, isExpanded, convertView, parent);
        } else {
            return setupUnregisterView(groupPosition, isExpanded, convertView, parent);
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = new TextView(mContext);
        return textView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    private View setupRegisterView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        switch (groupPosition) {
            case MeUtils.REGISTER_POSITION_LIVE:
                return setupLiveView(isExpanded, convertView, parent);
            case MeUtils.REGISTER_POSITION_WORKS_K:
                return setupWorksKView(isExpanded, convertView, parent);
            case MeUtils.REGISTER_POSITION_GROUP_MUSIC:
                return setupGroupMusicView(isExpanded, convertView, parent);
            case MeUtils.REGISTER_POSITION_PERSONAL_SONG_PAPER:
                return setupPersonalSongPaperView(isExpanded, convertView, parent);
            case MeUtils.REGISTER_POSITION_CONNECTION_SONG_PAPER:
                return setupConnectionSongPaperView(isExpanded, convertView, parent);
            case MeUtils.REGISTER_POSITION_PROMOTION:
                return setupPromotionView(isExpanded, convertView, parent);
        }
        return convertView;
    }

    private View setupUnregisterView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        switch (groupPosition) {
            case MeUtils.UNREGISTER_POSITION_WORKS_K:
                return setupWorksKView(isExpanded, convertView, parent);
            case MeUtils.UNREGISTER_POSITION_GROUP_MUSIC:
                return setupGroupMusicView(isExpanded, convertView, parent);
            case MeUtils.UNREGISTER_POSITION_PERSONAL_SONG_PAPER:
                return setupPersonalSongPaperView(isExpanded, convertView, parent);
            case MeUtils.UNREGISTER_POSITION_PROMOTION:
                return setupPromotionView(isExpanded, convertView, parent);
        }
        return convertView;
    }

    private View setupWorksKView(boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = mLInflater.inflate(R.layout.content_me_item_works_k, parent, false);
        convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                handleWorksKViewClick(view);
            }
        });
        return convertView;
    }

    private View setupGroupMusicView(boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = mLInflater.inflate(R.layout.content_me_item_group_music, parent, false);
        convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                handleGroupMusicViewClick(view);
            }
        });
        return convertView;
    }

    private View setupPersonalSongPaperView(boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = mLInflater.inflate(R.layout.content_me_item_personal_song_paper, parent, false);
        ImageView expandIv = convertView.findViewById(R.id.personal_song_paper_expanded);
        ImageView addIv = convertView.findViewById(R.id.personal_song_paper_add);
        if (isExpanded) {
            expandIv.setImageResource(R.drawable.ic_expanded);
        } else {
            expandIv.setImageResource(R.drawable.ic_unexpanded);
        }
        addIv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

            }
        });
        return convertView;
    }

    private View setupPromotionView(boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = mLInflater.inflate(R.layout.content_me_item_promotion, parent, false);
        convertView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                handlePromotionViewClick(view);
            }
        });
        return convertView;
    }

    private View setupLiveView(boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = mLInflater.inflate(R.layout.content_me_item_live, parent, false);
        return convertView;
    }

    private View setupConnectionSongPaperView(boolean isExpanded, View convertView, ViewGroup parent) {
        return convertView;
    }

    private void handleWorksKViewClick(View view) {
    }

    private void handleGroupMusicViewClick(View view) {
    }

    private void handlePromotionViewClick(View view) {
    }
}
